//
//  ZJUITools.m
//  ZiJinLian
//
//  Created by lc on 2017/3/21.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJUITools.h"

@implementation ZJUITools

//获得字体14 颜色#3
+(UILabel *)getLab_S14_C3{
    UILabel* lab =[[UILabel alloc]init];
    lab.textColor=TextColor3;
    lab.font=FontAndStyle14;
    return lab;
}

+(UILabel*)getLab_S14_C6{
    UILabel* lab =[[UILabel alloc]init];
    lab.textColor=TextColor6;
    lab.font=FontAndStyle14;
    return lab;
}

//获取白色14
+(UILabel*)getWhiteLab{
    UILabel* lab =[[UILabel alloc]init];
    lab.textColor=[UIColor whiteColor];
    lab.font=FontAndStyle14;
    return lab;
}

//获得btn单图片带文字
+(UIButton *)getBtnWithImage:(NSString *)name title:(NSString *)title{
    UIButton* btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
    btn.titleLabel.font=FontAndStyle14;
    btn.bsg_width=20+[title widthWithFont:FontAndStyle14];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    btn.bsg_height=40;
    return btn;

}

+(UIButton *)getBtnWithSelect:(NSString *)nor select:(NSString *)select title:(NSString *)title{
    UIButton* btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:nor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:select] forState:UIControlStateSelected];
    if (title) {
        [btn setTitle:title forState:UIControlStateNormal];
         btn.titleLabel.font=FontAndStyle14;
        [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
         btn.bsg_width=20+[title widthWithFont:FontAndStyle14];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    }else{
        btn.bsg_width=20;
    }
 
    btn.bsg_height=40;
    return btn;
}

#pragma mark 获取圆形imageView
+(UIImageView*)getCricelImageView:(CGFloat)cor{
    UIImageView* view =[[UIImageView alloc]init];
    view.layer.cornerRadius=cor;
    view.layer.masksToBounds=YES;
    return view;
}


//获得分割线
+(UIView *)getCutLine{
    UIView* line =[[UIView alloc]init];
    line.backgroundColor=RGB(246, 246, 246);
    return line;
}
//获得垂直分割线
+(UIView*)getHCutLine{
    UIView* line =[[UIView alloc]init];
    line.backgroundColor=RGB(246, 246, 246);
    return line;
}

//自定义的分割线
+(UIView *)getCutlineCustom:(CGFloat)height{
    UIView* line =[[UIView alloc]init];
    line.bsg_height=height;
    line.backgroundColor=RGB(246, 246, 246);
    line.bsg_width=MainW;
    return line;
}

+(UIButton*)getBottomBtn:(NSString*)title image:(NSString*)image{
     UIButton* btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor=button_Red_Color;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font=FontAndStyle14;
    if (image) {
        [btn setImage:[UIImage imageNamed:image] forState:0];
        btn.imageEdgeInsets=UIEdgeInsetsMake(0, -10, 0, 0);
    }
    return btn;
}
//获取空间 高度
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}
+(UIView *)showBlankView{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH)];
    view.backgroundColor=[UIColor whiteColor];
    UIImageView *imagView=[[UIImageView alloc]initWithFrame:CGRectMake(MainW/2-90, 75, 180, 210)];
    imagView.image=[UIImage imageNamed:@"img_blank_monk"];
    [view addSubview:imagView];
    MYLabel* label=[[MYLabel alloc]init];
    label.text=@"暂无数据";
    label.font=FontAndStyle14;
    CGSize size=[label.text sizeWithAttributes:@{NSFontAttributeName:label.font }];
    label.textColor=TextColor6;
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imagView.mas_bottom).offset(15);
        make.centerX.equalTo(view.mas_centerX);
        make.width.equalTo(size.width);
        make.height.equalTo(20);
    }];
    return view;
}
//获得搜索框
+(UISearchBar *)getSearchBar{
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.placeholder=MyString(@"请输入关键词");
    searchBar.frame = CGRectMake(0, 0, MainW-120, 28);
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.layer.cornerRadius = 15;
    [searchBar setTranslucent:YES];
    [searchBar setImage:[UIImage imageNamed:@"nav_search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    UITextField *textfield = [searchBar valueForKey:@"_searchField"];
    [textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    textfield.textAlignment=NSTextAlignmentLeft;
    [textfield setTextColor:[UIColor whiteColor]];
    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"tab_searchbg"] forState:UIControlStateNormal];
    searchBar.layer.masksToBounds = YES;
    searchBar.searchBarStyle = 0;
    return searchBar;
}
+(UIView *)customSearchbar{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW-120, 28)];
    //view.backgroundColor=[UIColor blackColor];
    //view.alpha=0.2;
    UIImageView*bgimag=[[UIImageView alloc]initWithFrame:view.frame];
    bgimag.image=[UIImage imageNamed:@"tab_searchbg"];
    [view addSubview:bgimag];
    UIImageView*imag=[[UIImageView alloc]initWithFrame:CGRectMake(15, 6, 16, 16)];
    imag.image=[UIImage imageNamed:@"nav_search"];
    [view addSubview:imag];
    UITextField *textfied=[[UITextField alloc]initWithFrame:CGRectMake(imag.bsg_right+8, 4, MainW-165, 20)];
    textfied.placeholder=MyString(@"请输入关键词");
    textfied.clearButtonMode=UITextFieldViewModeWhileEditing;
    [textfied setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textfied setValue:FontAndStyle14 forKeyPath:@"_placeholderLabel.font"];
    textfied.textAlignment=NSTextAlignmentLeft;
    [textfied setTextColor:[UIColor whiteColor]];
    textfied.font=FontAndStyle14;
    [view addSubview:textfied];
    ViewRadius(view, 14);
    return view;
}
//获得imageview
+(UIImageView *)getImage:(NSString *)name{
   
    return  [[UIImageView alloc]initWithImage:[UIImage imageNamed:name]];
}

//获得小箭头
+(UIImageView *)getArrowImage{
    UIImageView* arrow=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_arrow_right"]];
    arrow.contentMode=UIViewContentModeScaleAspectFit;
    return arrow;
}
//电话号码
+ (BOOL)checkTel:(NSString *)mobileNumbel
{
    /**
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,177,180,189,181(增加)
     */
    NSString * MOBIL = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[57])|(17[013678]))\\d{8}$";
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CT = @"^1((33|53|77|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    return NO;
}
#pragma mark判断输入的身份证号是否正确
+ (BOOL)checkIdentityCardNo:(NSString*)cardNo
{
    if (cardNo.length != 18) {
        return  NO;
    }
    
    NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2",nil];
    
    NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2",nil] forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil]];
    
    NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
    int val;
    BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
    if (!isNum) {
        return NO;
    }
    int sumValue = 0;
    for (int i =0; i<17; i++) {
        sumValue+=[[cardNo substringWithRange:NSMakeRange(i ,1) ]intValue]* [[codeArray objectAtIndex:i]intValue];
    }
    NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]];
    if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17,1)]uppercaseString]]) {
        return YES;
    }
    return  NO;
}



//检查银行卡号

+ (BOOL) checkBankCardNo:(NSString*) cardNo{
    int oddsum = 0;     //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = [[cardNo substringFromIndex:cardNoLength-1]intValue];
    
    cardNo = [cardNo substringToIndex:cardNoLength -1];
    for (int i = cardNoLength -1; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1,1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}
//转译带斜杠字符串
+(NSArray *)jsonDic:(NSMutableString *)string{
    NSMutableString *responseString = string;
    
    NSString *character = nil;
    
    for (int i = 0; i < responseString.length; i ++) {
        
        character = [responseString substringWithRange:NSMakeRange(i, 1)];
        
        if ([character isEqualToString:@"\\"])
            
            [responseString deleteCharactersInRange:NSMakeRange(i, 1)];
        
    }
    
    //将字符窜转化成字典
    
    
    
    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err;
    
    NSArray *configFirstDic = [NSJSONSerialization JSONObjectWithData:jsonData
                                    
                                                                   options:NSJSONReadingMutableContainers
                                    
                                                                     error:&err];
    return configFirstDic;
}

//改变字体颜色

+(UILabel *)changeStringColor:(UILabel *)lableText NSRang:(NSRange *)rang{
    NSMutableAttributedString *countString=[[NSMutableAttributedString alloc]initWithString:lableText.text];
    [countString addAttribute:NSForegroundColorAttributeName value:Red_Color range:NSMakeRange(3,lableText.text.length-3)];
    [countString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:*rang];
    lableText.attributedText=countString;
    return lableText;
}
// 根据图片url获取图片尺寸
+(CGSize)getImageSizeWithURL:(id)imageURL
{
    NSURL* URL = nil;
    if([imageURL isKindOfClass:[NSURL class]]){
        URL = imageURL;
    }
    if([imageURL isKindOfClass:[NSString class]]){
        URL = [NSURL URLWithString:imageURL];
    }
    if(URL == nil)
        return CGSizeZero;                  // url不正确返回CGSizeZero
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
    NSString* pathExtendsion = [URL.pathExtension lowercaseString];
    
    CGSize size = CGSizeZero;
    if([pathExtendsion isEqualToString:@"png"]){
        size =  [self getPNGImageSizeWithRequest:request];
    }
    else if([pathExtendsion isEqual:@"gif"])
    {
        size =  [self getGIFImageSizeWithRequest:request];
    }
    else{
        size = [self getJPGImageSizeWithRequest:request];
    }
    if(CGSizeEqualToSize(CGSizeZero, size))                    // 如果获取文件头信息失败,发送异步请求请求原图
    {
        NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
        UIImage* image = [UIImage imageWithData:data];
        if(image)
        {
            size = image.size;
        }
    }
    return size;
}
//  获取PNG图片的大小
+(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 8)
    {
        int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        [data getBytes:&w3 range:NSMakeRange(2, 1)];
        [data getBytes:&w4 range:NSMakeRange(3, 1)];
        int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
        [data getBytes:&h1 range:NSMakeRange(4, 1)];
        [data getBytes:&h2 range:NSMakeRange(5, 1)];
        [data getBytes:&h3 range:NSMakeRange(6, 1)];
        [data getBytes:&h4 range:NSMakeRange(7, 1)];
        int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}
//  获取gif图片的大小
+(CGSize)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 4)
    {
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        short w = w1 + (w2 << 8);
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(2, 1)];
        [data getBytes:&h2 range:NSMakeRange(3, 1)];
        short h = h1 + (h2 << 8);
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}
//  获取jpg图片的大小
+(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
    [request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    if ([data length] <= 0x58) {
        return CGSizeZero;
    }
    
    if ([data length] < 210) {// 肯定只有一个DQT字段
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
        [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
        short w = (w1 << 8) + w2;
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
        [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
        short h = (h1 << 8) + h2;
        return CGSizeMake(w, h);
    } else {
        short word = 0x0;
        [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
        if (word == 0xdb) {
            [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
            if (word == 0xdb) {// 两个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            } else {// 一个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            }
        } else {
            return CGSizeZero;
        }
    }
}


@end
